function Index()
{


}

Index.prototype={
		
		curClient:'',
		init:function()
		{
			index.getDeviceList();
			index.initPanel();
			
		},
		initPanel:function(){
			$('#left').width(200);
			$('#rightContent').width($(document).width()-250);
			//$('#result').height($(document).height()-50);
			
		},
		
		 
		getBaseInfo:function(){
			
			$.post('/web/service',{'action':'execCmd','param':obj2str({"cmd":"top -n 1 -b",'host':index.curClient})},function(data){ 
				
			var load=	/load average:([^\n]+?)\n/.exec(data)[1];
			
			var mem=	/Mem:([^\n]+?)\n/.exec(data)[1];
			
			var cpu=	/Cpu\(s\):([^\n]+?)\n/.exec(data)[1];

			
			$('#baseInfo').html('<div style="float:left;border:1px dashed;"><div id="loadavg"></div></div><div style="float:left;border:1px dashed;"><div id="memory"></div></div><div style="float:left;border:1px dashed;"><div id="cpu"></div></div><div style="float:left;border:1px dashed;"><div id="net"></div></div><div style="float:left;border:1px dashed;"><div id="disk"></div></div>');
			 
			
			var loads=load.split(/\,/ig);
			var mems=mem.split(/\,/ig);
			var cpus=cpu.split(/\,/ig);
			
			for(var i=0;i<mems.length;i++){
				mems[i]=$.trim(mems[i]);
			} 
			
			for(var i=0;i<cpus.length;i++){
				cpus[i]=$.trim(cpus[i]);
			}
			
			 
			
			var arrayOfData = new Array(
				  	 [loads[0],'one','#222222'],
				  	 [loads[1],'five','#7D252B'],
				  	 [loads[2],'fifteen','#EB9781'] 
				);

				$('#loadavg').jqbargraph({ 
				  	 data: arrayOfData ,
				  	legendWidth:50,
				  	width:200,
				  	
				  	title:'Load average'
				});
				
				var arrayOfData = new Array(
					  	 [mems[0].split(/\s+/ig)[0].replace('k',''),mems[0].split(/\s+/ig)[1],'#222222'],
					  	 [mems[1].split(/\s+/ig)[0].replace('k',''),mems[1].split(/\s+/ig)[1],'#7D252B'],
					  	 [mems[2].split(/\s+/ig)[0].replace('k',''),mems[2].split(/\s+/ig)[1],'#EB9781']
					);

					$('#memory').jqbargraph({
					  	 data: arrayOfData ,
					 	legendWidth:50,
					  	width:200,
					  	title:'Memory' 
					});
					
					
				var arrayOfData = new Array(
					  	 [cpus[0].match(/\d+[.]?\d+/)[0],'user','#222222'],
					  	 [cpus[1].match(/\d+[.]?\d+/)[0],'system','#7D252B'],
					  	 [cpus[3].match(/\d+[.]?\d+/)[0],'idle','#EB9781']
					);

					$('#cpu').jqbargraph({
					  	 data: arrayOfData ,
					 	legendWidth:50,
					  	width:200,
					  	 title:'CPU'  
					});
					
					
					$.post('/web/service',{'action':'execCmd','param':obj2str({"cmd":"df -m|awk '{if(NR>1) print $1\"(\"$2\"M)\"\"\t\"$5}'",'host':index.curClient})},function(data){

						
						var arrayOfData = new Array(  
							  	  
							);
						var rows=data.split(/\n/ig);
						
						for ( var i = 0; i < rows.length; i++) { 
							rows[i]=$.trim(rows[i]);
						}
						for ( var i = 0; i < rows.length; i++) {
							arrayOfData.push( [$.trim( rows[i].split(/\t/ig)[1]).replace('%',''),$.trim(rows[i].split(/\s+/ig)[0]),'#222222']);
						}  
						
						var w=arrayOfData.length*150; 
						
						$('#disk').jqbargraph({
						  	 data: arrayOfData , 
						  	 width:w,
						  	legendWidth:120, 
						  	 title:'Disk'   
						});
 
					});
					
					
					$.post('/web/service',{'action':'execCmd','param':obj2str({"cmd":"netstat -nat | awk 'FNR>2{print $NF}' | sort | uniq -c",'host':index.curClient})},function(data){
						
						var arrayOfData = new Array(
							  	 
							);
						var rows=data.split(/\n/ig);
						
						for ( var i = 0; i < rows.length; i++) {
							rows[i]=$.trim(rows[i]);
						}
						for ( var i = 0; i < rows.length; i++) {
							arrayOfData.push( [$.trim( rows[i].split(/\s+/ig)[0]),$.trim(rows[i].split(/\s+/ig)[1]),'#222222']);
						}  
						
						var w=arrayOfData.length*100;
						
						$('#net').jqbargraph({
						  	 data: arrayOfData , 
						  	 width:w,
						  	 title:'Net'  
						});
						
					}); 

			});
		},
		
		getDeviceList:function(){
			
			
			Web.postFill("/web/service",{"action":"getData","param": obj2str({"action":"getDeviceList"})},'#left',{'after':function(data){
				$('#left div').click(function(){
					$('#left div').css({'background':'#fff'});        //背景变白色
					index.curClient=$(this).text();                   //取当前HTML元素的文本内容
					//$('#left').css({'float':'left'});
					//index.getInfo();
					
					index.getBaseInfo();
					
					$(this).css({'background':'#ccc'});               //当前。。的背景色变灰色
				
				});
				
				if(index.curClient!=''){
					$('#left div:contains('+index.curClient+')').css({'background':'#ccc'}); 
					$('#left div:contains('+index.curClient+')').trigger('click');
				}
				
			}});
			

			
			
		},
		getInfo:function(){
			
			Web.postFill("/web/service",{"action":"getData","param": obj2str({"action":"getInfo"})},'#left',{'after':function(data){
				
				
				
				//JQBar图表
				//这里开始
				arrayOfData = new Array(
					  	 [10.3,'Jan','#222222'],
					  	 [15.2,'Feb','#7D252B'],
					  	 [13.1,'Mar','#EB9781'],
					  	 [16.3,'Apr','#FFD2B5'],
					  	 [14.5,'May','#4A4147']
					);

					$('#exampleSimple').jqbargraph({
					  	 data: arrayOfData 
					});
			    //这里结束
				
	//			alert(data);
			}});
			
		}
};

var index=new Index(); 

$(document).ready(function(){
	
	
	
	index.init();
	
	$(window).resize(function(){
		index.initPanel();
	});
	
	
	
	setInterval(index.getDeviceList, 10000);
	
	
	
	
	
	$('#cmdBtn').click(function(){
		$.post('/web/service',{'action':'execCmd','param':obj2str({"cmd":$('#cmd').val(),'host':index.curClient})},function(data){ 
			$('#result').val(data);
		});
		
	});
	
	
	$('#btnSetting').click(function(){
		
		var action=$(this).attr('action');
		
		if(index.curClient!='') {
			

			if(action=='load'){
				$.post('/web/service',{'action':'loadSetting','param':obj2str({'host':index.curClient})},function(data){ 
					
					try {
						eval('('+data+')');
					$('#result').val(data);
					$('#btnSetting').attr('action','save');
					$('#btnSetting').val('保存配置');
					$('#cmdBtn,#devices,#saveDevices,#javaBtn,#kievBtn,#btnDevice').attr('disabled','disabled');
					
					}catch (e) {
						
					}
				});
			} else if(action=='save') {
				try {
				eval('('+$('#result').val()+')');
				$.post('/web/service',{'action':'saveSetting','param':obj2str({"setting": encodeURIComponent($('#result').val()),'host':index.curClient})},function(data){ 
					$('#result').val(data);
					$('#btnSetting').attr('action','load');
					$('#btnSetting').val('修改配置');
					$('#cmdBtn,#devices,#saveDevices,#javaBtn,#kievBtn,#btnDevice').attr('disabled',''); 
				});
				}catch (e) { 
					alert('配置为JSON格式');
				}
			}
		
		} else {
			alert('请选择要配置的服务器');
		}
		
		$('#btnSetting').attr('disabled','');  
		
	});
	
	
	
	$('#btnDevice').click(function(){
		
		var action=$('#btnDevice').attr('action');
		
			if(action=='load'){
				$.post('/web/service',{'action':'getDevices','param':obj2str({"cmd":$('#cmd').val(),'host':index.curClient})},function(data){ 
					
					try {
					$('#result').val(data);
					$('#btnDevice').attr('action','save');
					$('#btnDevice').val('保存列表');
					$('#cmdBtn,#devices,#saveDevices,#javaBtn,#kievBtn,#btnSetting').attr('disabled','disabled');
					
					}catch (e) {
						
					}
				});
			} else if(action=='save') {
				try {
			
				$.post('/web/service',{'action':'saveDevices','param':obj2str({"clients":$('#result').val()})},function(data){ 
					$('#result').val(data);
					$('#btnDevice').attr('action','load');
					$('#btnDevice').val('监控列表');
					$('#cmdBtn,#devices,#saveDevices,#javaBtn,#kievBtn,#btnSetting').attr('disabled',''); 
				});
				}catch (e) { 
					alert('配置为JSON格式');
				}
			}
		

		
		$('#btnDevice').attr('disabled','');  
		
	});
	
	

	
	$('#kievBtn').click(function(){ 
		if($('#cmd').val().indexOf('kiev')>=0){
		} else { 
			$('#cmd').val('kiev zk 172.16.200.239:2181 /');
		}
		$.post('/web/service',{'action':'execCmd','param':obj2str({"cmd":$('#cmd').val(),'host':'172.16.10.99:1234'})},function(data){ 
			$('#result').val(data);
		});
		
	});
	
	$('#javaBtn').click(function(){ 
		if($('#cmd').val().indexOf('java')>=0){ 
		} else { 
			$('#cmd').val('ps -aux|grep java');
		}
		$.post('/web/service',{'action':'execCmd','param':obj2str({"cmd":$('#cmd').val(),'host':index.curClient})},function(data){ 
			$('#result').val(data); 
		});
		
	});
	
	
	
	
	
	
	
	
});